﻿using Kaskelot.SP.Model.Contracts;
using Kaskelot.SP.Services;
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Kaskelot.SP.Core;
using Kaskelot.SP.Model.Client;
using Kaskelot.SP.Services.Client.Mapping;

namespace Kaskelot.SP.UI.CONTROLTEMPLATES.Kaskelot.SP.UI.Forms
{
	public partial class KursTillfalleForm : BaseForm, IForm
	{
		public string Id { get; set; }
		public string ParentId { get; set; }
		public KursTillfalle Current { get; set; }
		public ClientPerson CurrentKursansvarig { get; set; }

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Page.IsPostBack)
				return;

			using (KursService svc = new KursService())
			{
				if (Id.IsGuid())
				{
					Current = svc.GetKursTillfalle(new Guid(Id));

					using (PersonService pSvc = new PersonService())
					{

						if (this.Current.Kursansvarig.HasValue)
						{
							CurrentKursansvarig = pSvc.GetPerson(this.Current.Kursansvarig.Value).ToClientContract();
							trCurrentKursansvarig.Visible = true;
						}
					}
				}
				else
				{
					Current = new KursTillfalle() { Kursdatum = DateTime.Today, Starttid = TimeSpan.FromHours(12), Sluttid = TimeSpan.FromHours(12) };
				}

				var v = svc.GetKurser().Select(k => new ListItem() { Text = k.Beskrivning, Value = k.Id.ToString() }).OrderBy(k => k.Text);
				KursRepeater.DataSource = v;
				KursRepeater.DataBind();
			}
		}

		public string GetStarttid()
		{
			return Current.Starttid.Hours + ":" + Current.Starttid.Minutes.ToString().PadLeft(2, '0');
		}

		public string GetSluttid()
		{
			return Current.Sluttid.Hours + ":" + Current.Sluttid.Minutes.ToString().PadLeft(2, '0');
		}
	}
}
